import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterstudyapplast/business/viewmodels/user_viewmodel.dart';
import 'package:flutterstudyapplast/services/service_locator.dart';
import 'package:toast/toast.dart';

class RegisterPage extends StatefulWidget {
  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  FocusNode pwd1Node = FocusNode();
  FocusNode pwd2Node = FocusNode();
  FocusNode usernameNode = FocusNode();
  var formKey = GlobalKey<FormState>();
  var ScaffoldKey = GlobalKey<ScaffoldState>();
  String _username;
  String _pwd1;
  String _pwd2;
  UserViewModel _userViewModel = serviceLocator<UserViewModel>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: ScaffoldKey,
      appBar: AppBar(title: Text("注册"),),
      body: Form(
        key: formKey,
        child: ListView(
          padding: EdgeInsets.all(20),
          children: <Widget>[
            _buildUsername(),
            _buildPwd1(),
            _buildPwd2(),
            _buildRegister()
          ],
        ),
      ),
    );
  }
  Widget _buildUsername(){
    return TextFormField(
      autofocus: true,
      textInputAction: TextInputAction.next,
      initialValue: _username,
      decoration: InputDecoration(labelText: "用户名"),
      onEditingComplete: (){
        FocusScope.of(context).requestFocus(pwd1Node);
      },
      validator: (value){
        if(value == null || value.trim().isEmpty){
          return "请输入用户名";
        }
        _username = value;
      },
    );
  }
  Widget _buildPwd1(){
    return TextFormField(
      focusNode: pwd1Node,
      textInputAction: TextInputAction.next,
      initialValue: _pwd1,
      decoration: InputDecoration(
          labelText: "密码",
      ),
      onEditingComplete: (){
        FocusScope.of(context).requestFocus(pwd2Node);
      },
      validator: (value){
        if(value == null || value.trim().isEmpty){
          return "请输入密码";
        }
        _pwd1 = value;
      },
    );
  }
  Widget _buildPwd2(){
    return TextFormField(
      focusNode: pwd2Node,
      textInputAction: TextInputAction.done,
      initialValue: _pwd2,
      decoration: InputDecoration(
        labelText: "确认密码",
      ),
      onEditingComplete: _register,
      validator: (value){
        if(value == null || value.trim().isEmpty){
          return "请输入确认密码";
        }else if(value.trim() != _pwd1){
          return "两次密码不一致";
        }
        _pwd2 = value;
      },
    );
  }
  Widget _buildRegister(){
    return Container(
      height: 45,
      color: Theme.of(context).primaryColor,
      child: RaisedButton(child:Text("注册",style: TextStyle(fontSize: 18,color: Colors.white),),onPressed: _register),
      margin: EdgeInsets.only(top:18,bottom: 18),
    );
  }

  _register() async{
    pwd1Node.unfocus();
    pwd2Node.unfocus();
    if(formKey.currentState.validate()){
      showDialog(context: context,barrierDismissible: false,builder: (context){
        return Center(child: CircularProgressIndicator(),);
      });
      //可以去注册
      var result =  await _userViewModel.register(_username,_pwd1);
      print(result);
      //对话框dismiss
      Navigator.pop(context);
      if(!result){

        //弹出提示
//        _scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(error),));
        Toast.show("注册失败", context,gravity: Toast.CENTER);
      }else{
        //3 种方案
        //1、直接使用第三方
        //2、利用Flutter自己实现
        //3、通过插件调用到android的toast
        Toast.show("注册成功!", context,gravity: Toast.CENTER);
        Navigator.pop(context);
      }
    }
  }
}
